<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/ftl/css/bootstrap.min.css">
    <link rel="stylesheet" href="/ftl/css/laypage.css">
    <script src="/ftl/js/vue/vue.min.js"></script>
    <script src="/ftl/js/jquery/jquery.min.js"></script>
    <script src="/ftl/js/laypage/laypage.js" charset="utf-8"></script>
    <script src="/ftl/js/layer/layer.js" charset="utf-8"></script>
</head>
<body>
<div id="app" class="container">

    <form class="form-inline bg-danger" role="form">
        查询条件：

        <div class="form-group">
            <label class="sr-only" for="bname">书名</label>
            <input type="text" class="form-control" id="bname" placeholder="书名">
        </div>
        <div class="form-group">
            <label class="sr-only" for="author">作者</label>
            <input type="text" class="form-control" id="author" placeholder="作者">
        </div>
        <button type="button" id="findUser" class="btn btn-success">查询图书</button>
        <button type="button" id="addUserBtn" class="btn btn-danger">增加图书</button>
    </form>


    <div class="table-responsive">
        <table class="table">
            <thead>
            <tr class="success">
                <td>序号</td>
                <td>书名</td>
                <td>作者</td>
                <td>出版社</td>
                <td>--</td>
                <td>操作</td>
            </tr>
            </thead>
            <tbody>
            <tr class="active" v-for="(item,index) in result">
                <td>{{index+1}}</td>
                <td>{{item.bname}}</td>
                <td>{{item.author}}</td>
                <td>{{item.publ}}</td>
                <td></td>
                <td><a href="#" @click="editBooks(item.bid)">编辑</a>
                    &nbsp
                    <a href="#" @click="delBooks(item.bid)">删除</a>
                </td>
            </tr>
            </tbody>
        </table>
        <div id="pagenav"></div>
    </div>
</div>

<script>

    var app = new Vue({
        el: "#app",
        data: {
            result: []
        }
    });
    //查询交互
    var getall = function (curr) {
        $.ajax({
            type: "POST",
            url: "/showBooks",
            dataType: "json",
            data: {
                pageSize: 5,
                pageNum: curr || 1,
                bname: $("#bname").val()
            },
            success: function (msg) {
                app.result = msg.page;
                laypage({
                    cont: "pagenav",
                    skin: "#00AAff",
                    groups:3,
                    pages: msg.totaPage,
                    first: "第一页",
                    last: "最后一页",
                    curr: curr || 1,
                    jump: function (i, first) {
                        if (!first) {
                            getall(i.curr)
                        }
                    }
                })
            }
        })

    };
    //页面加载调用
    getall();

    //查询
    $("#findUser").click(function () {
        getall();
    });

    //新增数据
    $("#addUserBtn").click(function () {
        layer.open({
            type: 2,
            title: "新增书籍",
            fix: false,
            anim:1,
            maxmin: true,
            shadeClose: true,
            area: ["1080px", "600px"],
            content: "book_add.html",
            end: function () {
                getall();
            }
        })
    });

    //删除操作
    var delBooks = function (id) {
        layer.confirm("确认删除?", {
                btn: ["是", "否"]
            }, function () {
                //是
                $.ajax({
                    type: "POST",
                    url: "delbook",
                    dataType: "json",
                    data: {
                        bid: id
                    },
                    success: function (msg) {
                        getall();
                        layer.msg("已成功删除!", {icon: 6});
                    }
                })
            }
            , function () {
                //否
            })
    };
    //编辑操作
    var editBooks = function (id) {
        layer.open({
            type: 2,
            title: "编辑书籍",
            fixed: true,
            anim:3,
            maxmin: true,
            shadeClose: true,
            area: ["1080px", "600px"],
            content: "update"+id,
            end: function () {
                getall();
            }
        })
    }


</script>

</body>
</html>